package com.loop.unclelend.utils.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.loop.unclelend.R;
import com.loop.unclelend.utils.ImageUtil;


public class Dialog_Loading extends Dialog {

    /**
     * LoadDialog
     */
    private static Dialog_Loading loadDialog;

    /**
     * 点击系统返回按钮是否消失
     */
    private final boolean isCancel;

    /**
     * the LoadDialog constructor
     *
     * @param ctx        Context
     * @param isTouchOut boolean : true-->返回不消失，false-->按返回dismiss
     */
    public Dialog_Loading(Context ctx, boolean isTouchOut, boolean isCancel, String msg) {
        super(ctx, R.style.DialogTheme);
//        this.getContext().setTheme(android.R.style.Theme_InputMethod);
        setContentView(R.layout.uncle_dialog_loading);
        ImageView imgLoading = findViewById(R.id.img_loading);
        ImageUtil.Companion.getInstance().loadImageRes(ctx,imgLoading,R.mipmap.anim_loading);

        /**
         * 加载中文字
         */
        TextView txtLoading = findViewById(R.id.txt_loading);
        if (msg.isEmpty()){
            txtLoading.setVisibility(View.GONE);
        } else {
            txtLoading.setVisibility(View.VISIBLE);
            txtLoading.setText(msg);
        }

        /* 点击返回按钮是否dismiss */
        this.isCancel = isCancel;
        /* 点击外部是否dismiss */
        setCanceledOnTouchOutside(isTouchOut);

        Window window = getWindow();
        WindowManager.LayoutParams attributesParams = window.getAttributes();
        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.5f;//背景透明度
//        attributesParams.dimAmount = 0f;//背景透明度
        window.setAttributes(attributesParams);
        window.setBackgroundDrawableResource(android.R.color.transparent);// 一句话搞定

        window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!this.isCancel) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * show the mDialogTextView
     *
     * @param context
     */
    public static void show(Context context,String msg) {
        showDialog(context, false, true,msg);
    }

    /**
     * show the mDialogTextView
     *
     * @param context  Context
     * @param isCancel boolean
     */
    public static void show(Context context, boolean isTouchOut, boolean isCancel,String msg) {
        showDialog(context, isTouchOut, isCancel,msg);
    }

    /**
     * show the mDialogTextView
     *
     * @param context    Context
     * @param isTouchOut boolean, true is can't dimiss，false is can dimiss
     */
    private static void showDialog(Context context, boolean isTouchOut, boolean isCancel,String msg) {
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return;
            }
        }
        if (loadDialog != null && loadDialog.isShowing()) {
            return;
        }
        loadDialog = new Dialog_Loading(context, isTouchOut, isCancel,msg);
        loadDialog.show();
    }

    /**
     * dismiss the mDialogTextView
     */
    public static void dismiss(Context context) {
        try {
            if (context instanceof Activity) {
                if (((Activity) context).isFinishing()) {
                    loadDialog = null;
                    return;
                }
            }

            if (loadDialog != null && loadDialog.isShowing()) {
                Context loadContext = loadDialog.getContext();
                if (loadContext instanceof Activity) {
                    if (((Activity) loadContext).isFinishing()) {
                        loadDialog = null;
                        return;
                    }
                }
                loadDialog.dismiss();
                loadDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            loadDialog = null;
        }
    }
}
